class register{
    constructor(){
        //获取
        this.us = this.$('#us');
        this.ps = this.$('#ps');
        this.sure = this.$('#sure');
        this.code = this.$('#idcode');
        this.ipt_1 = this.$('#ipt_1');
         //数组
        this.arr = [false,false,false];
        // 添加事件
        this.add();
    }

       
       
       
    add(){
        var that = this;
        this.us.onblur = function(){
            let re = /0?(13|14|15|17|18|19)[0-9]{9}/;
            let us = this.value;
            if(re.test(us)){
                this.style.borderColor = '';
                 that.arr[0] = true;
            }else{
                this.style.borderColor = 'red';
                 that.arr[0] = false;
            }
        }
        this.ps.onblur = function(){
            let re = /^\w{6,20}/;
            let ps = this.value;
            if(re.test(ps)){
                this.style.borderColor = '';
                 that.arr[1] = true;
            }else{
                this.style.borderColor = 'red';
                 that.arr[1] = false;
            }
        }
        this.sure.onblur = function(){
            let re = that.ps.value;
            let sure = this.value;
            if(re === sure){
                this.style.borderColor = '';
                 that.arr[2] = true;
            }else{
                this.style.borderColor = 'red';
                 that.arr[2] = false;
            }
        }
        // ipt[3].onblur = function(){
        //     var re = code.setCode;
        //     var str = this.value;
        //     if(re === str){
        //         this.style.borderColor = '';
        //          that.arr[3] = true;
        //     }else{
        //         this.style.borderColor = 'red';
        //          that.arr[3] = false;
        //     }
        // }
        this.ipt_1.onclick = function(){
            if(that.arr.indexOf(false) === -1){
                //获取用户名
                let us = that.us.value;
                //获取密码
                let ps = that.ps.value;
                //cookie中是否有当前注册的用户
                // registers = {us : ps,us : ps}
                //获取cookie
                let cookie = new Cookie();
                //将获取到的数据统一为String类型
                let cookie_str = cookie.getCookie('registers') ? cookie.getCookie('registers') : '';
                //转为对象
                let cookie_obj = that.convertStrToObj(cookie_str);
                //判断对象中是否有当前注册的用户
                if(us in cookie_obj){
                    alert('用户名已存在！');
                }else{
                    cookie_obj[us] = ps;
                    //存入cookie
                    cookie.cookie('registers',JSON.stringify(cookie_obj),{expires : 7,path : '/'});
                    alert('注册成功!');
                    location.href = 'login.html';
                }
            }else{
                alert('请完善信息！');
            }
            // if(arr.indexOf(false) === -1){
            //     alert('注册成功');
            //     location.href = 'login.html';
            // }else{
            //     alert('请完善信息');
            // }
        }
    }
    $(selector){
        return document.querySelector(selector);
    }
    convertStrToObj(str){
        if(!str){
            return {};
        }
        return JSON.parse(str);
    }
}      
new register();      
  